package com.example.controller;

import com.example.common.CustomException;
import com.example.common.Result;
import com.example.entity.Fav;
import com.example.service.FavService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 收藏(Fav)表控制层
 *
 */
@RestController
@RequestMapping("/fav")
public class FavController {
    
    @Resource
    private FavService favService;

    //信息列表
    @ResponseBody
    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, @RequestParam(defaultValue = "1",value = "pageSize") Integer pageSize, HttpServletRequest request){
        String key = request.getParameter("key");
        String memberid = request.getParameter("memberid");
        
        PageInfo<Fav> pageInfo = favService.getFavList(pageNum, pageSize, key, memberid);
        return Result.success(pageInfo);
    }
    
    //添加或编辑信息
    @ResponseBody
    @RequestMapping("/add")
    public Result add(Fav fav, HttpServletRequest request) {
        String message = favService.addOrCancelFav(fav);
        if (message != null) {
            throw new CustomException(message);
        }
        return Result.success();
    }
    //删除信息
    @ResponseBody
    @RequestMapping("/del")
    public Result del(int id, HttpServletRequest request) {
        favService.deleteFav(id);
        return Result.success();
    }
    
}

